import { type ImportAppMeta } from "@cdyun/types"
import { type PluginOption } from "vite"
import vue from "@vitejs/plugin-vue"
import vueJsx from "@vitejs/plugin-vue-jsx"
import { configUnocssPlugin } from "./unocss"
import { configCompressPlugin } from "./compress"
import { configVisualizerPlugin } from "./visualizer"
import { configComponentsPlugin } from "./components"
import { configAutoImportPlugin } from "./auto-import"
import { configIconsPlugin } from "./icons"
import { configHtmlPlugin } from "./html"
import { configSvgIconsPlugin } from "./svg-icons"

/**
 * 配置Vite插件
 *
 * @param env 导入的元对象
 * @returns 插件数组
 */
export async function setupVitePlugins(env: ImportAppMeta, rootPath: string, isBuild: boolean) {
  const plugins: PluginOption[] = [
    vue(),
    vueJsx()
  ]

  //组件自动按需引入
  plugins.push(configComponentsPlugin(rootPath))

  //自动导入 Composition API
  plugins.push(configAutoImportPlugin(rootPath))

  // icon
  plugins.push(configIconsPlugin())

  // html
  plugins.push(configHtmlPlugin(env))

  // unocss
  plugins.push(configUnocssPlugin())

  //生成 svg 雪碧图
  plugins.push(configSvgIconsPlugin(rootPath, isBuild))

  if (isBuild) {
    // 压缩
    plugins.push(configCompressPlugin())
    // 性能分析
    plugins.push(configVisualizerPlugin())
  }
  return plugins
}